package com.bbx.taxi.client.Util;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;

import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.comm.returns.line.Cities;
import com.bbx.api.sdk.model.comm.returns.line.To_Cities;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.MyOrderList;
import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.DB.LineDB;
import com.bbx.taxi.client.Bean.Line.CitiesBean.AppCitysBean.ChildCitysBeanX;
import com.bbx.taxi.client.Bean.Line.CitiesBean.AppCitysBean.ChildCitysBeanX.ToCitiesBean;
import com.bbx.taxi.client.Bean.Line.CitiesBean.AppCitysBean.ChildCitysBeanX.ToCitiesBean.ChildCitysBean;
import com.bbx.taxi.client.Bean.Line.CitiesBean.AppCitysBean;

public class Tel {
	/**
	 * 拨打客服电话
	 */
	public static void onTel_KF(final Context context, final MyOrderList mMyOrderList) {
		new AsyncTask<Void, Void, Void>() {
			String tel_kf = "";
			
			@Override
			protected void onPreExecute() {
				ToastUtil.showToast(context, R.string.tel_kf);
			}

			@Override
			protected Void doInBackground(Void... arg0) {
				LineDB db_line = new LineDB();
				String start_name = mMyOrderList.getLocations().getStart().getAddress();
				String start_city = "";
				if (start_name.contains(" ")) {
					start_city = start_name.substring(0, start_name.indexOf(" "));
				} else if (start_name.contains("\t")) {
					start_city = start_name.substring(0, start_name.indexOf("\t"));
				} else if (start_name.contains("-")) {
					start_city = start_name.substring(0, start_name.indexOf("-"));
				} else {
					start_city = start_name;
				}
				String end_name = mMyOrderList.getLocations().getEnd().getAddress();
				String end_city = "";
				if (end_name.contains(" ")) {
					end_city = end_name.substring(0, end_name.indexOf(" "));
				} else if (end_name.contains("\t")) {
					end_city = end_name.substring(0, end_name.indexOf("\t"));
				} else if (end_name.contains("-")) {
					end_city = end_name.substring(0, end_name.indexOf("-"));
				} else {
					end_city = end_name;
				}
				ChildCitysBean mChildCitysBean=ToCity2.getEndChildCity(start_city, end_city, true);
				try{tel_kf=mChildCitysBean.getIncity_phone();}catch (Exception e){}
				return null;
			}

			@Override
			protected void onPostExecute(Void result) {
				 
				//用线路id获取
				if(tel_kf==null||tel_kf.equals("")) {
					onTel_KF(context,mMyOrderList.getLine_id(),mMyOrderList.getOrder_type() == Value.ORDER_TYPE_SN);
				}else{
					// 用intent启动拨打电话
					context.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
							+ tel_kf)));
				}
				super.onPostExecute(result);
			}
			
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}

	/**
	 * 拨打客服电话,传递线路id
	 */
	public static void onTel_KF(final Context context, final String line_id,
			final boolean isInCity) {
		new AsyncTask<Void, Void, Void>() {
			String tel_kf = "";
			@Override
			protected Void doInBackground(Void... arg0) {
				tel_kf=ToCity2.getPhone(line_id,isInCity);
				return null;
			}
			@Override
			protected void onPostExecute(Void result) {
				if(tel_kf==null||tel_kf.equals("")) {
					switch (VersionUtils.getVersionType(context)){
						case bbx:
							context.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
									+ Value.TEL_KF)));
							break;
						case tianshang:
							context.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
									+ Value.TEL_TSX)));
							break;
					}
				}else{
					// 用intent启动拨打电话
					context.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
							+ tel_kf)));
				}
				super.onPostExecute(result);
			}
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	

		
	}
}
